﻿Public Partial Class Wfrm_Recepcionar_Solicitud
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'session("usuario")
        If Not Page.IsPostBack Then
            ViewState("usu") = Session("usuario")
            carga_bodega()
            FK_EMPRESA()
            'CreaDatatable()
            BuscarSolicitudes()

            ' txtSolicitud.ReadOnly = True
            ' txtSolicitud.UpdateAfterCallBack = True
            cmbBodega.Enabled = False
            cmbBodega.UpdateAfterCallBack = True
            Carga_Datos_Usuario()
        End If
    End Sub
    Sub Carga_Datos_Usuario()
        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence
        rp.StoredProcedure = "SP_OC_Get_INFO_FORM"
        rp.AddParametro(ViewState("usu"))
        exec.ExecuteDataTable(rp)

        If rp.TieneDatos Then
            lbl_clinica.Text = rp.DtTable.Rows(0).Item("tNombreFantasia").ToString.Trim
            lbl_usuario.Text = rp.DtTable.Rows(0).Item("Tnombre").ToString.Trim
            lbl_fecha.Text = Now

            lbl_clinica.UpdateAfterCallBack = True
            lbl_usuario.UpdateAfterCallBack = True
            lbl_fecha.UpdateAfterCallBack = True

        End If

    End Sub
    Sub CreaDatatable() 'JQ
        Dim Dt2 As New DataTable
        Dt2.Columns.Add("")
        Dt2.Columns.Add("nCodigo_PKI")
        Dt2.Columns.Add("Producto")
        Dt2.Columns.Add("tNombre")
        Dt2.Columns.Add("CANT_SOLICITADA")
        ViewState("DT") = Dt2
    End Sub
    Sub FK_EMPRESA()
        ViewState("FK_EMPRESA") = Session("empresa")

        'Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        'Dim rp As New DotNetResponse.SQLPersistence

        'rp.StoredProcedure = ("SP_OC_GET_FK_EMPRESA")
        'rp.AddParametro(ViewState("usu"))
        'exec.ExecuteDataTable(rp)

        'If rp.TieneDatos Then
        '    ViewState("FK_EMPRESA") = rp.DtTable.Rows(0).Item(0).ToString
        'End If

    End Sub

    Sub carga_bodega()
        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence

        rp.StoredProcedure = ("SP_OC_CARGA_BODEGA_ALL") '01/07/2011 se elimina filtro usuario del procedimiento almacenado y se deja en servidor de las lilas
        rp.AddParametro(ViewState("usu"))
        exec.ExecuteDataTable(rp)

        If rp.TieneDatos Then
            cmbBodega.DataTextField = "DESCRIPCION"
            cmbBodega.DataValueField = "ID_BODEGA"
            cmbBodega.DataSource = rp.DtTable
            cmbBodega.DataBind()
            cmbBodega.UpdateAfterCallBack = True
        End If
    End Sub


    Private Function SumarProducto(ByVal codProducto As String) As Decimal 'JQ
        Dim i As Integer
        For i = 0 To DataGrid2.Items.Count - 1
            If codProducto = DataGrid2.Items(i).Cells(1).Text Then
                Return DirectCast(DirectCast(DataGrid2.Items(i).Cells(4).FindControl("Label5"), System.Web.UI.Control), Label).Text

            End If
        Next
        Return 0
    End Function

    Sub AgregaProducto(ByVal fk_solicitud As String, ByVal fk_producto As String, ByVal Cant_sol As Decimal) 'JQ

        Dim exec As New DotNetDal.SQLPersistence(True, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence

        rp.StoredProcedure = "SP_OC_ADD_SOLICITUD_PRODUCTO"

        rp.AddParametro(fk_solicitud)
        rp.AddParametro(fk_producto)
        rp.AddParametro(Cant_sol)
        rp.AddParametro(ViewState("usu"))

        exec.ExecuteNonQuery(rp)
        exec.Commit()

        If rp.Errores Then

        End If
    End Sub
    Sub ActualizaProducto(ByVal fk_solicitud As String, ByVal fk_producto As String, ByVal Cant_sol As Decimal) 'JQ

        Dim exec As New DotNetDal.SQLPersistence(True, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence

        rp.StoredProcedure = "SP_OC_UPDATE_SOLICITUD_PRODUCTO"

        rp.AddParametro(fk_solicitud)
        rp.AddParametro(fk_producto)
        rp.AddParametro(Cant_sol)
        rp.AddParametro(ViewState("usu"))

        exec.ExecuteNonQuery(rp)
        exec.Commit()

        If rp.Errores Then

        End If
    End Sub

    Sub GetProductosSolicitud()  'JQ   
        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence
        rp.StoredProcedure = "SP_OC_GET_SOLICITUD_PRODUCTO"
        rp.AddParametro(txtSolicitud.Text)
        exec.ExecuteDataTable(rp)

        ViewState("DT") = rp.DtTable
        If rp.TieneDatos Then
            ViewState("datos") = rp.DtTable.Rows(0).Item(4).ToString
            DataGrid2.DataSource = rp.DtTable
            DataGrid2.DataBind()
            DataGrid2.Enabled = False
            DataGrid2.UpdateAfterCallBack = True
        Else
            DataGrid2.DataSource = Nothing
            DataGrid2.DataBind()
            DataGrid2.Enabled = False
            DataGrid2.UpdateAfterCallBack = True

        End If

        'rp.ClearParametro()
        'rp.TieneDatos = False
        'rp.StoredProcedure = "SP_OC_GET_SOLICITUD_PRODUCTO_OC"
        'rp.AddParametro(txtSolicitud.Text)
        'exec.ExecuteDataTable(rp)

        'If rp.TieneDatos Then
        '    DataGrid4.DataSource = rp.DtTable
        '    DataGrid4.DataBind()
        '    DataGrid4.Enabled = False
        '    DataGrid4.UpdateAfterCallBack = True
        'Else
        '    DataGrid4.DataSource = Nothing
        '    DataGrid4.DataBind()
        '    DataGrid4.Enabled = False
        '    DataGrid4.UpdateAfterCallBack = True
        'End If
        'If rp.Errores Then

        'End If
    End Sub
    Protected Sub DataGrid2_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DataGrid2.SelectedIndexChanged
        Dim exec As New DotNetDal.SQLPersistence(True, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence
        If Val(txtSolicitud.Text) = 0 Then
            Dim i As Integer
            For i = 0 To ViewState("DT").Rows.Count - 1
                If ViewState("DT").Rows(i).Item(1) = DataGrid2.SelectedItem.Cells(1).Text Then  'cmbProducto.SelectedValue Then DU
                    ViewState("DT").Rows(i).Delete()
                    Exit For
                End If
            Next

            DataGrid2.DataSource = ViewState("DT")
            DataGrid2.DataBind()
            DataGrid2.UpdateAfterCallBack = True

        Else
            'se quita fila directamente de la tabla

            For i = 0 To ViewState("DT").Rows.Count - 1
                If ViewState("DT").Rows(i).Item(0) = DataGrid2.SelectedItem.Cells(1).Text Then  'cmbProducto.SelectedValue Then DU
                    ViewState("DT").Rows(i).Delete()
                    Exit For
                End If
            Next

            'hacer sp pa eliminar de la tabla  --- parecido al update 

            rp.StoredProcedure = "SP_OC_DELETE_PRODUCTO"
            rp.AddParametro(DataGrid2.SelectedItem.Cells(1).Text)
            rp.AddParametro(DirectCast(DataGrid1.SelectedItem.Cells(0).Controls(1), Anthem.LinkButton).Text)
            'DataGrid1.SelectedItem.Cells(0).Text)
            exec.ExecuteNonQuery(rp)
            exec.Commit()
            If rp.Errores Then

            End If

            'Cargar grilla
            GetProductosSolicitud()

        End If


    End Sub

    Protected Sub DataGrid1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles DataGrid1.SelectedIndexChanged
        txtSolicitud.Text = DataGrid1.SelectedItem.Cells(6).Text
        txtSolicitud.UpdateAfterCallBack = True
        lblclinica.Text = DataGrid1.SelectedItem.Cells(7).Text
        lblclinica.UpdateAfterCallBack = True

        txt_Observacion.Text = DataGrid1.SelectedItem.Cells(8).Text.Trim.Replace("&nbsp;", "")
        txt_Observacion.UpdateAfterCallBack = True

        GetProductosSolicitud()

        cmbBodega.SelectedValue = DataGrid1.SelectedItem.Cells(5).Text
        cmbBodega.UpdateAfterCallBack = True
        cmbBodega.Enabled = False
        cmbBodega.UpdateAfterCallBack = True
        pnl_mensaje.Visible = False
        pnl_mensaje.UpdateAfterCallBack = True

    End Sub

    Sub BuscarSolicitudes()
        Dim exec As New DotNetDal.SQLPersistence(False, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
        Dim rp As New DotNetResponse.SQLPersistence
        rp.StoredProcedure = "SP_OC_GET_SOLICITUD_PARA_ACEPTACION"
        exec.ExecuteDataTable(rp)
        If rp.TieneDatos Then
            Panel3.Visible = True
            Panel3.UpdateAfterCallBack = True
            DataGrid1.DataSource = rp.DtTable
            DataGrid1.DataBind()
            DataGrid1.UpdateAfterCallBack = True

            'For i = 0 To DataGrid1.Items.Count - 1
            '    DataGrid1.Items(i).Cells(2).Text = DataGrid1.Items(i).Cells(2).Text.Substring(6, 2) & "-" & DataGrid1.Items(i).Cells(2).Text.Substring(4, 2) & "-" & DataGrid1.Items(i).Cells(2).Text.Substring(0, 4)  'Convert.ToDateTime(DataGrid1.Items(i).Cells(1).Text).ToString("dd-MM-yyyy")
            '    DataGrid1.UpdateAfterCallBack = True
            'Next
 
        Else
            'Mensaje0("Wfrm_CreaSolicitud", "ValNumSol")
            DataGrid1.DataSource = Nothing
            DataGrid1.DataBind()
            DataGrid1.UpdateAfterCallBack = True
        End If


    End Sub
    Sub Mensaje(ByVal msg As String)

        If Anthem.Manager.IsCallBack Then
            Anthem.Manager.AddScriptForClientSideEval("alert('" & msg & "')")
        Else
            Page.ClientScript.RegisterStartupScript(Me.GetType, "alertar", "<script>alert('" & msg & "')</script>")
        End If

    End Sub


    Sub Mensaje0(ByVal tbl As String, ByVal codigo As String)
        Dim mensaje As String = FuncionesGlobal.GetMensaje(tbl, codigo)
        If Anthem.Manager.IsCallBack Then
            Anthem.Manager.AddScriptForClientSideEval("alert('" & mensaje & "')")
        Else
            Page.ClientScript.RegisterStartupScript(Me.GetType, "alertar", "<script>alert('" & mensaje & "')</script>")
        End If

    End Sub
    Sub Mensaje(ByVal tbl As String, ByVal codigo As String)
        Dim mensaje As String = FuncionesGlobal.GetMensaje(tbl, codigo)
        If Anthem.Manager.IsCallBack Then
            Anthem.Manager.AddScriptForClientSideEval("alert('" & mensaje & "')")
        Else
            Page.ClientScript.RegisterStartupScript(Me.GetType, "alertar", "<script>alert('" & mensaje & "')</script>")
        End If

    End Sub


    Protected Sub btnNuevaSol_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnNuevaSol.Click
        Dim i As Integer

        Dim Solicitud As String = ""

        For i = 0 To DataGrid1.Items.Count - 1
            If DirectCast(DataGrid1.Items(i).Cells(0).Controls(1), Anthem.CheckBox).Checked Then
                'Cambiar estado a la solicitud, ACEPTADA.
                'DBO.SP_OC_UPDATE_OC_SOLICITUD_ACEPTADA()
                '@FK_SOLICITUD NUMERIC(18,0)  
                ',@USUARIO VARCHAR(30)
                Dim exec As New DotNetDal.SQLPersistence(True, "cnn", DotNetDal.SQLPersistence.Provider.SQLCLIENT)
                Dim rp As New DotNetResponse.SQLPersistence
                rp.StoredProcedure = "SP_OC_UPDATE_OC_SOLICITUD_ACEPTADA"
                rp.AddParametro(DataGrid1.Items(i).Cells(6).Text)
                rp.AddParametro(ViewState("usu"))
                exec.ExecuteNonQuery(rp)
                exec.Commit()

                Solicitud = DataGrid1.Items(i).Cells(6).Text & "," & Solicitud

                '
            End If
        Next

        If Solicitud.Length > 0 Then

            Mensaje(" Solicitudes Aceptadas : " & Mid(Solicitud, 1, Solicitud.Length - 1))
        Else
            Mensaje(" NO se han Aceptadado Solicitudes ")
        End If


        BuscarSolicitudes()

    End Sub

End Class
